#!/bin/bash

function rootfs_install()
{
	echo "[$(basename $0)] ${FUNCNAME} $@"

	local TMPROOTFS=tmprootfs
	mkdir -p ${TMPROOTFS}/etc/
	mkdir -p ${TMPROOTFS}/opt/rk/

	echo -e '#!/bin/bash\n' >  ${TMPROOTFS}/etc/rc.local
	sed "s/_AUD_DEV_PLACEHOLDER_/jetsonxaviernxa/g" rc.local.nx.audio \
							>> ${TMPROOTFS}/etc/rc.local
	sed "s/_AUD_DEV_PLACEHOLDER_/jetsonxaviernxa/g" jetson_aplay.sh \
						> ${TMPROOTFS}/opt/rk/jetson_aplay.sh
	sed "s/_AUD_DEV_PLACEHOLDER_/jetsonxaviernxa/g" jetson_arecord.sh \
						> ${TMPROOTFS}/opt/rk/jetson_arecord.sh
	cp ./aud_picker.sh.nx ${TMPROOTFS}/opt/rk/aud_picker.sh
	cp ./song-youth.wav   ${TMPROOTFS}/opt/rk/
	cp ./gpio_pwr12v_out.sh ${TMPROOTFS}/opt/rk/

	sed "s/can_port_num=2/can_port_num=1/g" jetson_can.sh \
					> ${TMPROOTFS}/opt/rk/jetson_can.sh
	sed "s/RESOLUTION=1920x1080/RESOLUTION=1600x1300/g" jetson_camera.sh \
					> ${TMPROOTFS}/opt/rk/jetson_camera.sh

	local TMPROOTFS_PATH=$(abs_path "${TMPROOTFS}")
	local TMPROOTFS_PATH_LEVEL=$(abs_path_level "${TMPROOTFS_PATH}")
	traverse ${TMPROOTFS_PATH} traverse_filter_none 0 traverse_exec_install ${TMPROOTFS_PATH_LEVEL} ${L4T_TOP}/rootfs
	rm -rf ${TMPROOTFS}
}

function rootfs_uninstall()
{
	echo "[$(basename $0)] ${FUNCNAME} $@"

	sudo_cmd "rm -rf ${L4T_TOP}/rootfs/etc/rc.local \
					 ${L4T_TOP}/rootfs/opt/rk"
}

#############################################################

if [[ $# -le 0 ]]; then
	exit 0
fi

case $1 in
	"install" )
		rootfs_install
		;;
	"uninstall" )
		rootfs_uninstall
		;;
esac
